﻿/*******************************************************
*类名称：WechatAutoReplyManager
*版本号：V1.0.0.0
*作者：成俊杰
*CLR版本：4.0.30319.36264
*创建时间：2017-09-18 13:42:30
*说明：T_WechatAutoReply仓储类
*更新备注：
**********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wechat.Domain;
using Wechat.Dto.Extension;
using Wechat.Dto.Request;
using Wechat.Util.ParmsConfig;

namespace Wechat.Dao.Implement
{
    /// <summary>
    /// T_WechatAutoReply
    /// </summary>
    public class WechatAutoReplyRepository : RepositoryBase<WechatAutoReply>, IWechatAutoReplyRepository
    {
        /// <summary>
        /// 添加关注时默认回复
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public int InsertOrUpdateAutoReply(WechatAutoReplyRequest request)
        {
            var result = 0;
            WechatAutoReply model = null;
            if (request.AutoReplyId == 0)
            {
                if ((request.CompanyId ?? 0) == 0)
                    model = base.GetAll().OrderByDescending(reply => reply.AutoReplyId).FirstOrDefault();
                else
                    model = base.GetByKey("T_WechatAutoReply", "CompanyId", request.CompanyId)
                        .OrderByDescending(reply => reply.AutoReplyId).FirstOrDefault();
            }
            else
            {
                model = this.Get(request.AutoReplyId);//获取信息
            }
            if (model != null)
            {
                model.CompanyId = request.CompanyId;
                model.Content = request.Content;
                model.GroupId = request.GroupId;
                model.ImageTextId = request.ImageTextId;
                model.ReplyType = request.ReplyType;
                result = Update(model) ? model.AutoReplyId : 0;
            }
            else
            {
                model = request.ToModel();
                model.AutoReplyId = Insert(model);//添加信息
                result = model.AutoReplyId;
            }
            return result;
        }

    }
}